﻿#region Using directives

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;

using ATA.Entities;
using ATA.Data;

#endregion

namespace ATA.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="ChietKhauDacBietDsProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract partial class ChietKhauDacBietDsProviderBaseCore : EntityProviderBase<ATA.Entities.ChietKhauDacBietDs, ATA.Entities.ChietKhauDacBietDsKey>
	{		
		#region Get from Many To Many Relationship Functions
		#endregion	
		
		#region Delete Methods

		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to delete.</param>
		/// <returns>Returns true if operation suceeded.</returns>
		public override bool Delete(TransactionManager transactionManager, ATA.Entities.ChietKhauDacBietDsKey key)
		{
			return Delete(transactionManager, key.ChietKhauDacBietDsid);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="_chietKhauDacBietDsid">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public bool Delete(System.Int32 _chietKhauDacBietDsid)
		{
			return Delete(null, _chietKhauDacBietDsid);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_chietKhauDacBietDsid">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public abstract bool Delete(TransactionManager transactionManager, System.Int32 _chietKhauDacBietDsid);		
		
		#endregion Delete Methods
		
		#region Get By Foreign Key Functions
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauDacBietDS_ChietKhau key.
		///		FK_ChietKhauDacBietDS_ChietKhau Description: 
		/// </summary>
		/// <param name="_chietKhauId"></param>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauDacBietDs objects.</returns>
		public TList<ChietKhauDacBietDs> GetByChietKhauId(System.Int32 _chietKhauId)
		{
			int count = -1;
			return GetByChietKhauId(_chietKhauId, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauDacBietDS_ChietKhau key.
		///		FK_ChietKhauDacBietDS_ChietKhau Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_chietKhauId"></param>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauDacBietDs objects.</returns>
		/// <remarks></remarks>
		public TList<ChietKhauDacBietDs> GetByChietKhauId(TransactionManager transactionManager, System.Int32 _chietKhauId)
		{
			int count = -1;
			return GetByChietKhauId(transactionManager, _chietKhauId, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauDacBietDS_ChietKhau key.
		///		FK_ChietKhauDacBietDS_ChietKhau Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_chietKhauId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauDacBietDs objects.</returns>
		public TList<ChietKhauDacBietDs> GetByChietKhauId(TransactionManager transactionManager, System.Int32 _chietKhauId, int start, int pageLength)
		{
			int count = -1;
			return GetByChietKhauId(transactionManager, _chietKhauId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauDacBietDS_ChietKhau key.
		///		fkChietKhauDacBietDsChietKhau Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_chietKhauId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauDacBietDs objects.</returns>
		public TList<ChietKhauDacBietDs> GetByChietKhauId(System.Int32 _chietKhauId, int start, int pageLength)
		{
			int count =  -1;
			return GetByChietKhauId(null, _chietKhauId, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauDacBietDS_ChietKhau key.
		///		fkChietKhauDacBietDsChietKhau Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_chietKhauId"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauDacBietDs objects.</returns>
		public TList<ChietKhauDacBietDs> GetByChietKhauId(System.Int32 _chietKhauId, int start, int pageLength,out int count)
		{
			return GetByChietKhauId(null, _chietKhauId, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauDacBietDS_ChietKhau key.
		///		FK_ChietKhauDacBietDS_ChietKhau Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_chietKhauId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauDacBietDs objects.</returns>
		public abstract TList<ChietKhauDacBietDs> GetByChietKhauId(TransactionManager transactionManager, System.Int32 _chietKhauId, int start, int pageLength, out int count);
		
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauDacBietDS_LoaiThoiGian key.
		///		FK_ChietKhauDacBietDS_LoaiThoiGian Description: 
		/// </summary>
		/// <param name="_loaiThoiGianId">1: Thang</param>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauDacBietDs objects.</returns>
		public TList<ChietKhauDacBietDs> GetByLoaiThoiGianId(System.Byte _loaiThoiGianId)
		{
			int count = -1;
			return GetByLoaiThoiGianId(_loaiThoiGianId, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauDacBietDS_LoaiThoiGian key.
		///		FK_ChietKhauDacBietDS_LoaiThoiGian Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_loaiThoiGianId">1: Thang</param>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauDacBietDs objects.</returns>
		/// <remarks></remarks>
		public TList<ChietKhauDacBietDs> GetByLoaiThoiGianId(TransactionManager transactionManager, System.Byte _loaiThoiGianId)
		{
			int count = -1;
			return GetByLoaiThoiGianId(transactionManager, _loaiThoiGianId, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauDacBietDS_LoaiThoiGian key.
		///		FK_ChietKhauDacBietDS_LoaiThoiGian Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_loaiThoiGianId">1: Thang</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauDacBietDs objects.</returns>
		public TList<ChietKhauDacBietDs> GetByLoaiThoiGianId(TransactionManager transactionManager, System.Byte _loaiThoiGianId, int start, int pageLength)
		{
			int count = -1;
			return GetByLoaiThoiGianId(transactionManager, _loaiThoiGianId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauDacBietDS_LoaiThoiGian key.
		///		fkChietKhauDacBietDsLoaiThoiGian Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_loaiThoiGianId">1: Thang</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauDacBietDs objects.</returns>
		public TList<ChietKhauDacBietDs> GetByLoaiThoiGianId(System.Byte _loaiThoiGianId, int start, int pageLength)
		{
			int count =  -1;
			return GetByLoaiThoiGianId(null, _loaiThoiGianId, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauDacBietDS_LoaiThoiGian key.
		///		fkChietKhauDacBietDsLoaiThoiGian Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_loaiThoiGianId">1: Thang</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauDacBietDs objects.</returns>
		public TList<ChietKhauDacBietDs> GetByLoaiThoiGianId(System.Byte _loaiThoiGianId, int start, int pageLength,out int count)
		{
			return GetByLoaiThoiGianId(null, _loaiThoiGianId, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauDacBietDS_LoaiThoiGian key.
		///		FK_ChietKhauDacBietDS_LoaiThoiGian Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_loaiThoiGianId">1: Thang</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauDacBietDs objects.</returns>
		public abstract TList<ChietKhauDacBietDs> GetByLoaiThoiGianId(TransactionManager transactionManager, System.Byte _loaiThoiGianId, int start, int pageLength, out int count);
		
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauDacBietDS_NhomSanPham key.
		///		FK_ChietKhauDacBietDS_NhomSanPham Description: 
		/// </summary>
		/// <param name="_nhomSanPhamId"></param>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauDacBietDs objects.</returns>
		public TList<ChietKhauDacBietDs> GetByNhomSanPhamId(System.Int32 _nhomSanPhamId)
		{
			int count = -1;
			return GetByNhomSanPhamId(_nhomSanPhamId, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauDacBietDS_NhomSanPham key.
		///		FK_ChietKhauDacBietDS_NhomSanPham Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_nhomSanPhamId"></param>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauDacBietDs objects.</returns>
		/// <remarks></remarks>
		public TList<ChietKhauDacBietDs> GetByNhomSanPhamId(TransactionManager transactionManager, System.Int32 _nhomSanPhamId)
		{
			int count = -1;
			return GetByNhomSanPhamId(transactionManager, _nhomSanPhamId, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauDacBietDS_NhomSanPham key.
		///		FK_ChietKhauDacBietDS_NhomSanPham Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_nhomSanPhamId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauDacBietDs objects.</returns>
		public TList<ChietKhauDacBietDs> GetByNhomSanPhamId(TransactionManager transactionManager, System.Int32 _nhomSanPhamId, int start, int pageLength)
		{
			int count = -1;
			return GetByNhomSanPhamId(transactionManager, _nhomSanPhamId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauDacBietDS_NhomSanPham key.
		///		fkChietKhauDacBietDsNhomSanPham Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_nhomSanPhamId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauDacBietDs objects.</returns>
		public TList<ChietKhauDacBietDs> GetByNhomSanPhamId(System.Int32 _nhomSanPhamId, int start, int pageLength)
		{
			int count =  -1;
			return GetByNhomSanPhamId(null, _nhomSanPhamId, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauDacBietDS_NhomSanPham key.
		///		fkChietKhauDacBietDsNhomSanPham Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_nhomSanPhamId"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauDacBietDs objects.</returns>
		public TList<ChietKhauDacBietDs> GetByNhomSanPhamId(System.Int32 _nhomSanPhamId, int start, int pageLength,out int count)
		{
			return GetByNhomSanPhamId(null, _nhomSanPhamId, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauDacBietDS_NhomSanPham key.
		///		FK_ChietKhauDacBietDS_NhomSanPham Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_nhomSanPhamId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauDacBietDs objects.</returns>
		public abstract TList<ChietKhauDacBietDs> GetByNhomSanPhamId(TransactionManager transactionManager, System.Int32 _nhomSanPhamId, int start, int pageLength, out int count);
		
		#endregion

		#region Get By Index Functions
		
		/// <summary>
		/// 	Gets a row from the DataSource based on its primary key.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to retrieve.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <returns>Returns an instance of the Entity class.</returns>
		public override ATA.Entities.ChietKhauDacBietDs Get(TransactionManager transactionManager, ATA.Entities.ChietKhauDacBietDsKey key, int start, int pageLength)
		{
			return GetByChietKhauDacBietDsid(transactionManager, key.ChietKhauDacBietDsid, start, pageLength);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key PK_ChietKhauDacBietDS index.
		/// </summary>
		/// <param name="_chietKhauDacBietDsid"></param>
		/// <returns>Returns an instance of the <see cref="ATA.Entities.ChietKhauDacBietDs"/> class.</returns>
		public ATA.Entities.ChietKhauDacBietDs GetByChietKhauDacBietDsid(System.Int32 _chietKhauDacBietDsid)
		{
			int count = -1;
			return GetByChietKhauDacBietDsid(null,_chietKhauDacBietDsid, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ChietKhauDacBietDS index.
		/// </summary>
		/// <param name="_chietKhauDacBietDsid"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="ATA.Entities.ChietKhauDacBietDs"/> class.</returns>
		public ATA.Entities.ChietKhauDacBietDs GetByChietKhauDacBietDsid(System.Int32 _chietKhauDacBietDsid, int start, int pageLength)
		{
			int count = -1;
			return GetByChietKhauDacBietDsid(null, _chietKhauDacBietDsid, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ChietKhauDacBietDS index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_chietKhauDacBietDsid"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="ATA.Entities.ChietKhauDacBietDs"/> class.</returns>
		public ATA.Entities.ChietKhauDacBietDs GetByChietKhauDacBietDsid(TransactionManager transactionManager, System.Int32 _chietKhauDacBietDsid)
		{
			int count = -1;
			return GetByChietKhauDacBietDsid(transactionManager, _chietKhauDacBietDsid, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ChietKhauDacBietDS index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_chietKhauDacBietDsid"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="ATA.Entities.ChietKhauDacBietDs"/> class.</returns>
		public ATA.Entities.ChietKhauDacBietDs GetByChietKhauDacBietDsid(TransactionManager transactionManager, System.Int32 _chietKhauDacBietDsid, int start, int pageLength)
		{
			int count = -1;
			return GetByChietKhauDacBietDsid(transactionManager, _chietKhauDacBietDsid, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ChietKhauDacBietDS index.
		/// </summary>
		/// <param name="_chietKhauDacBietDsid"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="ATA.Entities.ChietKhauDacBietDs"/> class.</returns>
		public ATA.Entities.ChietKhauDacBietDs GetByChietKhauDacBietDsid(System.Int32 _chietKhauDacBietDsid, int start, int pageLength, out int count)
		{
			return GetByChietKhauDacBietDsid(null, _chietKhauDacBietDsid, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ChietKhauDacBietDS index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_chietKhauDacBietDsid"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="ATA.Entities.ChietKhauDacBietDs"/> class.</returns>
		public abstract ATA.Entities.ChietKhauDacBietDs GetByChietKhauDacBietDsid(TransactionManager transactionManager, System.Int32 _chietKhauDacBietDsid, int start, int pageLength, out int count);
						
		#endregion "Get By Index Functions"
	
		#region Custom Methods
		
		
		#endregion

		#region Helper Functions	
		
		/// <summary>
		/// Fill a TList&lt;ChietKhauDacBietDs&gt; From a DataReader.
		/// </summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">number of rows.</param>
		/// <returns>a <see cref="TList&lt;ChietKhauDacBietDs&gt;"/></returns>
		public static TList<ChietKhauDacBietDs> Fill(IDataReader reader, TList<ChietKhauDacBietDs> rows, int start, int pageLength)
		{
			NetTiersProvider currentProvider = DataRepository.Provider;
            bool useEntityFactory = currentProvider.UseEntityFactory;
            bool enableEntityTracking = currentProvider.EnableEntityTracking;
            LoadPolicy currentLoadPolicy = currentProvider.CurrentLoadPolicy;
			Type entityCreationFactoryType = currentProvider.EntityCreationalFactoryType;
			
			// advance to the starting row
			for (int i = 0; i < start; i++)
			{
				if (!reader.Read())
				return rows; // not enough rows, just return
			}
			for (int i = 0; i < pageLength; i++)
			{
				if (!reader.Read())
					break; // we are done
					
				string key = null;
				
				ATA.Entities.ChietKhauDacBietDs c = null;
				if (useEntityFactory)
				{
					key = new System.Text.StringBuilder("ChietKhauDacBietDs")
					.Append("|").Append((System.Int32)reader[((int)ChietKhauDacBietDsColumn.ChietKhauDacBietDsid - 1)]).ToString();
					c = EntityManager.LocateOrCreate<ChietKhauDacBietDs>(
					key.ToString(), // EntityTrackingKey
					"ChietKhauDacBietDs",  //Creational Type
					entityCreationFactoryType,  //Factory used to create entity
					enableEntityTracking); // Track this entity?
				}
				else
				{
					c = new ATA.Entities.ChietKhauDacBietDs();
				}
				
				if (!enableEntityTracking ||
					c.EntityState == EntityState.Added ||
					(enableEntityTracking &&
					
						(
							(currentLoadPolicy == LoadPolicy.PreserveChanges && c.EntityState == EntityState.Unchanged) ||
							(currentLoadPolicy == LoadPolicy.DiscardChanges && c.EntityState != EntityState.Unchanged)
						)
					))
				{
					c.SuppressEntityEvents = true;
					c.ChietKhauDacBietDsid = (System.Int32)reader[((int)ChietKhauDacBietDsColumn.ChietKhauDacBietDsid - 1)];
					c.NhomSanPhamId = (System.Int32)reader[((int)ChietKhauDacBietDsColumn.NhomSanPhamId - 1)];
					c.MucThuong = (System.Byte)reader[((int)ChietKhauDacBietDsColumn.MucThuong - 1)];
					c.GhiChu = (reader.IsDBNull(((int)ChietKhauDacBietDsColumn.GhiChu - 1)))?null:(System.String)reader[((int)ChietKhauDacBietDsColumn.GhiChu - 1)];
					c.ChietKhauId = (System.Int32)reader[((int)ChietKhauDacBietDsColumn.ChietKhauId - 1)];
					c.LastUpdate = (System.DateTime)reader[((int)ChietKhauDacBietDsColumn.LastUpdate - 1)];
					c.UpdateBy = (System.Int32)reader[((int)ChietKhauDacBietDsColumn.UpdateBy - 1)];
					c.LoaiThoiGianId = (System.Byte)reader[((int)ChietKhauDacBietDsColumn.LoaiThoiGianId - 1)];
					c.EntityTrackingKey = key;
					c.AcceptChanges();
					c.SuppressEntityEvents = false;
				}
				rows.Add(c);
			}
		return rows;
		}		
		/// <summary>
		/// Refreshes the <see cref="ATA.Entities.ChietKhauDacBietDs"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="ATA.Entities.ChietKhauDacBietDs"/> object to refresh.</param>
		public static void RefreshEntity(IDataReader reader, ATA.Entities.ChietKhauDacBietDs entity)
		{
			if (!reader.Read()) return;
			
			entity.ChietKhauDacBietDsid = (System.Int32)reader[((int)ChietKhauDacBietDsColumn.ChietKhauDacBietDsid - 1)];
			entity.NhomSanPhamId = (System.Int32)reader[((int)ChietKhauDacBietDsColumn.NhomSanPhamId - 1)];
			entity.MucThuong = (System.Byte)reader[((int)ChietKhauDacBietDsColumn.MucThuong - 1)];
			entity.GhiChu = (reader.IsDBNull(((int)ChietKhauDacBietDsColumn.GhiChu - 1)))?null:(System.String)reader[((int)ChietKhauDacBietDsColumn.GhiChu - 1)];
			entity.ChietKhauId = (System.Int32)reader[((int)ChietKhauDacBietDsColumn.ChietKhauId - 1)];
			entity.LastUpdate = (System.DateTime)reader[((int)ChietKhauDacBietDsColumn.LastUpdate - 1)];
			entity.UpdateBy = (System.Int32)reader[((int)ChietKhauDacBietDsColumn.UpdateBy - 1)];
			entity.LoaiThoiGianId = (System.Byte)reader[((int)ChietKhauDacBietDsColumn.LoaiThoiGianId - 1)];
			entity.AcceptChanges();
		}
		
		/// <summary>
		/// Refreshes the <see cref="ATA.Entities.ChietKhauDacBietDs"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="ATA.Entities.ChietKhauDacBietDs"/> object.</param>
		public static void RefreshEntity(DataSet dataSet, ATA.Entities.ChietKhauDacBietDs entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.ChietKhauDacBietDsid = (System.Int32)dataRow["ChietKhauDacBietDSID"];
			entity.NhomSanPhamId = (System.Int32)dataRow["NhomSanPhamID"];
			entity.MucThuong = (System.Byte)dataRow["MucThuong"];
			entity.GhiChu = Convert.IsDBNull(dataRow["GhiChu"]) ? null : (System.String)dataRow["GhiChu"];
			entity.ChietKhauId = (System.Int32)dataRow["ChietKhauID"];
			entity.LastUpdate = (System.DateTime)dataRow["LastUpdate"];
			entity.UpdateBy = (System.Int32)dataRow["UpdateBy"];
			entity.LoaiThoiGianId = (System.Byte)dataRow["LoaiThoiGianID"];
			entity.AcceptChanges();
		}
		#endregion 
		
		#region DeepLoad Methods
		/// <summary>
		/// Deep Loads the <see cref="IEntity"/> object with criteria based of the child 
		/// property collections only N Levels Deep based on the <see cref="DeepLoadType"/>.
		/// </summary>
		/// <remarks>
		/// Use this method with caution as it is possible to DeepLoad with Recursion and traverse an entire object graph.
		/// </remarks>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="entity">The <see cref="ATA.Entities.ChietKhauDacBietDs"/> object to load.</param>
		/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
		/// <param name="deepLoadType">DeepLoadType Enumeration to Include/Exclude object property collections from Load.</param>
		/// <param name="childTypes">ATA.Entities.ChietKhauDacBietDs Property Collection Type Array To Include or Exclude from Load</param>
		/// <param name="innerList">A collection of child types for easy access.</param>
	    /// <exception cref="ArgumentNullException">entity or childTypes is null.</exception>
	    /// <exception cref="ArgumentException">deepLoadType has invalid value.</exception>
		public override void DeepLoad(TransactionManager transactionManager, ATA.Entities.ChietKhauDacBietDs entity, bool deep, DeepLoadType deepLoadType, System.Type[] childTypes, DeepSession innerList)
		{
			if(entity == null)
				return;

			#region ChietKhauIdSource	
			if (CanDeepLoad(entity, "ChietKhau|ChietKhauIdSource", deepLoadType, innerList) 
				&& entity.ChietKhauIdSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.ChietKhauId;
				ChietKhau tmpEntity = EntityManager.LocateEntity<ChietKhau>(EntityLocator.ConstructKeyFromPkItems(typeof(ChietKhau), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.ChietKhauIdSource = tmpEntity;
				else
					entity.ChietKhauIdSource = DataRepository.ChietKhauProvider.GetByChietKhauId(transactionManager, entity.ChietKhauId);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'ChietKhauIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.ChietKhauIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.ChietKhauProvider.DeepLoad(transactionManager, entity.ChietKhauIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion ChietKhauIdSource

			#region LoaiThoiGianIdSource	
			if (CanDeepLoad(entity, "LoaiThoiGian|LoaiThoiGianIdSource", deepLoadType, innerList) 
				&& entity.LoaiThoiGianIdSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.LoaiThoiGianId;
				LoaiThoiGian tmpEntity = EntityManager.LocateEntity<LoaiThoiGian>(EntityLocator.ConstructKeyFromPkItems(typeof(LoaiThoiGian), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.LoaiThoiGianIdSource = tmpEntity;
				else
					entity.LoaiThoiGianIdSource = DataRepository.LoaiThoiGianProvider.GetByLoaiThoiGianId(transactionManager, entity.LoaiThoiGianId);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'LoaiThoiGianIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.LoaiThoiGianIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.LoaiThoiGianProvider.DeepLoad(transactionManager, entity.LoaiThoiGianIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion LoaiThoiGianIdSource

			#region NhomSanPhamIdSource	
			if (CanDeepLoad(entity, "NhomSanPham|NhomSanPhamIdSource", deepLoadType, innerList) 
				&& entity.NhomSanPhamIdSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.NhomSanPhamId;
				NhomSanPham tmpEntity = EntityManager.LocateEntity<NhomSanPham>(EntityLocator.ConstructKeyFromPkItems(typeof(NhomSanPham), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.NhomSanPhamIdSource = tmpEntity;
				else
					entity.NhomSanPhamIdSource = DataRepository.NhomSanPhamProvider.GetByNhomSanPhamId(transactionManager, entity.NhomSanPhamId);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'NhomSanPhamIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.NhomSanPhamIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.NhomSanPhamProvider.DeepLoad(transactionManager, entity.NhomSanPhamIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion NhomSanPhamIdSource
			
			//used to hold DeepLoad method delegates and fire after all the local children have been loaded.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			// Deep load child collections  - Call GetByChietKhauDacBietDsid methods when available
			
			#region ChietKhauDbdsChuongTrinhCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<ChietKhauDbdsChuongTrinh>|ChietKhauDbdsChuongTrinhCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'ChietKhauDbdsChuongTrinhCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.ChietKhauDbdsChuongTrinhCollection = DataRepository.ChietKhauDbdsChuongTrinhProvider.GetByChietKhauDacBietDsid(transactionManager, entity.ChietKhauDacBietDsid);

				if (deep && entity.ChietKhauDbdsChuongTrinhCollection.Count > 0)
				{
					deepHandles.Add("ChietKhauDbdsChuongTrinhCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<ChietKhauDbdsChuongTrinh>) DataRepository.ChietKhauDbdsChuongTrinhProvider.DeepLoad,
						new object[] { transactionManager, entity.ChietKhauDbdsChuongTrinhCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			//Fire all DeepLoad Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			deepHandles = null;
		}
		
		#endregion 
		
		#region DeepSave Methods

		/// <summary>
		/// Deep Save the entire object graph of the ATA.Entities.ChietKhauDacBietDs object with criteria based of the child 
		/// Type property array and DeepSaveType.
		/// </summary>
		/// <param name="transactionManager">The transaction manager.</param>
		/// <param name="entity">ATA.Entities.ChietKhauDacBietDs instance</param>
		/// <param name="deepSaveType">DeepSaveType Enumeration to Include/Exclude object property collections from Save.</param>
		/// <param name="childTypes">ATA.Entities.ChietKhauDacBietDs Property Collection Type Array To Include or Exclude from Save</param>
		/// <param name="innerList">A Hashtable of child types for easy access.</param>
		public override bool DeepSave(TransactionManager transactionManager, ATA.Entities.ChietKhauDacBietDs entity, DeepSaveType deepSaveType, System.Type[] childTypes, DeepSession innerList)
		{	
			if (entity == null)
				return false;
							
			#region Composite Parent Properties
			//Save Source Composite Properties, however, don't call deep save on them.  
			//So they only get saved a single level deep.
			
			#region ChietKhauIdSource
			if (CanDeepSave(entity, "ChietKhau|ChietKhauIdSource", deepSaveType, innerList) 
				&& entity.ChietKhauIdSource != null)
			{
				DataRepository.ChietKhauProvider.Save(transactionManager, entity.ChietKhauIdSource);
				entity.ChietKhauId = entity.ChietKhauIdSource.ChietKhauId;
			}
			#endregion 
			
			#region LoaiThoiGianIdSource
			if (CanDeepSave(entity, "LoaiThoiGian|LoaiThoiGianIdSource", deepSaveType, innerList) 
				&& entity.LoaiThoiGianIdSource != null)
			{
				DataRepository.LoaiThoiGianProvider.Save(transactionManager, entity.LoaiThoiGianIdSource);
				entity.LoaiThoiGianId = entity.LoaiThoiGianIdSource.LoaiThoiGianId;
			}
			#endregion 
			
			#region NhomSanPhamIdSource
			if (CanDeepSave(entity, "NhomSanPham|NhomSanPhamIdSource", deepSaveType, innerList) 
				&& entity.NhomSanPhamIdSource != null)
			{
				DataRepository.NhomSanPhamProvider.Save(transactionManager, entity.NhomSanPhamIdSource);
				entity.NhomSanPhamId = entity.NhomSanPhamIdSource.NhomSanPhamId;
			}
			#endregion 
			#endregion Composite Parent Properties

			// Save Root Entity through Provider
			if (!entity.IsDeleted)
				this.Save(transactionManager, entity);
			
			//used to hold DeepSave method delegates and fire after all the local children have been saved.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
	
			#region List<ChietKhauDbdsChuongTrinh>
				if (CanDeepSave(entity.ChietKhauDbdsChuongTrinhCollection, "List<ChietKhauDbdsChuongTrinh>|ChietKhauDbdsChuongTrinhCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(ChietKhauDbdsChuongTrinh child in entity.ChietKhauDbdsChuongTrinhCollection)
					{
						if(child.ChietKhauDacBietDsidSource != null)
						{
							child.ChietKhauDacBietDsid = child.ChietKhauDacBietDsidSource.ChietKhauDacBietDsid;
						}
						else
						{
							child.ChietKhauDacBietDsid = entity.ChietKhauDacBietDsid;
						}

					}

					if (entity.ChietKhauDbdsChuongTrinhCollection.Count > 0 || entity.ChietKhauDbdsChuongTrinhCollection.DeletedItems.Count > 0)
					{
						//DataRepository.ChietKhauDbdsChuongTrinhProvider.Save(transactionManager, entity.ChietKhauDbdsChuongTrinhCollection);
						
						deepHandles.Add("ChietKhauDbdsChuongTrinhCollection",
						new KeyValuePair<Delegate, object>((DeepSaveHandle< ChietKhauDbdsChuongTrinh >) DataRepository.ChietKhauDbdsChuongTrinhProvider.DeepSave,
							new object[] { transactionManager, entity.ChietKhauDbdsChuongTrinhCollection, deepSaveType, childTypes, innerList }
						));
					}
				} 
			#endregion 
				
			//Fire all DeepSave Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			
			// Save Root Entity through Provider, if not already saved in delete mode
			if (entity.IsDeleted)
				this.Save(transactionManager, entity);
				

			deepHandles = null;
						
			return true;
		}
		#endregion
	} // end class
	
	#region ChietKhauDacBietDsChildEntityTypes
	
	///<summary>
	/// Enumeration used to expose the different child entity types 
	/// for child properties in <c>ATA.Entities.ChietKhauDacBietDs</c>
	///</summary>
	public enum ChietKhauDacBietDsChildEntityTypes
	{
		
		///<summary>
		/// Composite Property for <c>ChietKhau</c> at ChietKhauIdSource
		///</summary>
		[ChildEntityType(typeof(ChietKhau))]
		ChietKhau,
			
		///<summary>
		/// Composite Property for <c>LoaiThoiGian</c> at LoaiThoiGianIdSource
		///</summary>
		[ChildEntityType(typeof(LoaiThoiGian))]
		LoaiThoiGian,
			
		///<summary>
		/// Composite Property for <c>NhomSanPham</c> at NhomSanPhamIdSource
		///</summary>
		[ChildEntityType(typeof(NhomSanPham))]
		NhomSanPham,
	
		///<summary>
		/// Collection of <c>ChietKhauDacBietDs</c> as OneToMany for ChietKhauDbdsChuongTrinhCollection
		///</summary>
		[ChildEntityType(typeof(TList<ChietKhauDbdsChuongTrinh>))]
		ChietKhauDbdsChuongTrinhCollection,
	}
	
	#endregion ChietKhauDacBietDsChildEntityTypes
	
	#region ChietKhauDacBietDsFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;ChietKhauDacBietDsColumn&gt;"/> class
	/// that is used exclusively with a <see cref="ChietKhauDacBietDs"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class ChietKhauDacBietDsFilterBuilder : SqlFilterBuilder<ChietKhauDacBietDsColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the ChietKhauDacBietDsFilterBuilder class.
		/// </summary>
		public ChietKhauDacBietDsFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the ChietKhauDacBietDsFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public ChietKhauDacBietDsFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the ChietKhauDacBietDsFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public ChietKhauDacBietDsFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion ChietKhauDacBietDsFilterBuilder
	
	#region ChietKhauDacBietDsParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;ChietKhauDacBietDsColumn&gt;"/> class
	/// that is used exclusively with a <see cref="ChietKhauDacBietDs"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class ChietKhauDacBietDsParameterBuilder : ParameterizedSqlFilterBuilder<ChietKhauDacBietDsColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the ChietKhauDacBietDsParameterBuilder class.
		/// </summary>
		public ChietKhauDacBietDsParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the ChietKhauDacBietDsParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public ChietKhauDacBietDsParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the ChietKhauDacBietDsParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public ChietKhauDacBietDsParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion ChietKhauDacBietDsParameterBuilder
	
	#region ChietKhauDacBietDsSortBuilder
    
    /// <summary>
    /// A strongly-typed instance of the <see cref="SqlSortBuilder&lt;ChietKhauDacBietDsColumn&gt;"/> class
	/// that is used exclusively with a <see cref="ChietKhauDacBietDs"/> object.
    /// </summary>
    [CLSCompliant(true)]
    public class ChietKhauDacBietDsSortBuilder : SqlSortBuilder<ChietKhauDacBietDsColumn>
    {
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the ChietKhauDacBietDsSqlSortBuilder class.
		/// </summary>
		public ChietKhauDacBietDsSortBuilder() : base() { }

		#endregion Constructors

    }    
    #endregion ChietKhauDacBietDsSortBuilder
	
} // end namespace
